Tidy and fix bindings for the SR class.
authorEwan Mellor <ewan@xensource.com>
Wed, 27 Dec 2006 11:49:59 +0000 (11:49 +0000)
committerEwan Mellor <ewan@xensource.com>
Wed, 27 Dec 2006 11:49:59 +0000 (11:49 +0000)
Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/python/xen/xend/XendAPI.py
tools/python/xen/xend/XendStorageRepository.py

index e52c2ead3cd72b19519728fa29dacccc92f6faa0..e7215e56af68c6b99fb015284b54cb0cccb6cb31 100644 (file)
@@ -100,6 +100,7 @@ def catch_typeerror(func):
         try:
             return func(self, *args, **kwargs)
         except TypeError, exn:
+            #log.exception('catch_typeerror')
             if hasattr(func, 'api') and func.api in argcounts:
                 # Assume that if the exception was thrown inside this
                 # file, then it is due to an invalid call from the client,
@@ -1446,37 +1447,40 @@ class XendAPI:
         return xen_api_success(sr.get_record())
 
     # Attribute acceess
-    def SR_get_VDIs(self, session, sr_ref):
-        sr = XendNode.instance().get_sr()
-        return xen_api_success(sr.list_images())
 
-    def SR_get_virtual_allocation(self, session, sr_ref):
-        sr = XendNode.instance().get_sr()        
-        return sr.used_space_bytes()
+    def _get_SR_func(self, _, func, conv = None):
+        result = getattr(XendNode.instance().get_sr(), func)()
+        if conv:
+            result = conv(result)
+        return xen_api_success(result)
+
+    def _get_SR_attr(self, _, attr):
+        return xen_api_success(str(getattr(XendNode.instance().get_sr(),
+                                           attr)))
+
+    def SR_get_VDIs(self, _, ref):
+        return self._get_SR_func(ref, 'list_images')
+
+    def SR_get_virtual_allocation(self, _, ref):
+        return self._get_SR_func(ref, 'virtual_allocation', str)
 
-    def SR_get_physical_utilisation(self, session, sr_ref):
-        sr = XendNode.instance().get_sr()        
-        return sr.used_space_bytes()
+    def SR_get_physical_utilisation(self, _, ref):
+        return self._get_SR_func(ref, 'used_space_bytes', str)
 
-    def SR_get_physical_size(self, session, sr_ref):
-        sr = XendNode.instance().get_sr()        
-        return sr.total_space_bytes()
+    def SR_get_physical_size(self, _, ref):
+        return self._get_SR_func(ref, 'total_space_bytes', str)
     
-    def SR_get_type(self, session, sr_ref):
-        sr = XendNode.instance().get_sr()
-        return xen_api_success(sr.type)
+    def SR_get_type(self, _, ref):
+        return self._get_SR_attr(ref, 'type')
 
-    def SR_get_location(self, session, sr_ref):
-        sr = XendNode.instance().get_sr()
-        return xen_api_success(sr.location)
+    def SR_get_location(self, _, ref):
+        return self._get_SR_attr(ref, 'location')
 
-    def SR_get_name_label(self, session, sr_ref):
-        sr = XendNode.instance().get_sr()
-        return xen_api_success(sr.name_label)      
+    def SR_get_name_label(self, _, ref):
+        return self._get_SR_attr(ref, 'name_label')
     
-    def SR_get_name_description(self, session, sr_ref):
-        sr = XendNode.instance().get_sr()
-        return xen_api_success(sr.name_description)        
+    def SR_get_name_description(self, _, ref):
+        return self._get_SR_attr(ref, 'name_description')
 
     def SR_set_name_label(self, session, sr_ref, value):
         sr = XendNode.instance().get_sr()
index 42568e84a7c19e0d7c7848bd9afec5f76d3f1088..3ab5480a814927fe3cc56c0115c3f11ed2a6fc88 100644 (file)
@@ -294,7 +294,11 @@ class XendStorageRepository:
         """
         self.lock.acquire()
         try:
-            return self.storage_max
+            if self.storage_max == XEND_STORAGE_NO_MAXIMUM:
+                stfs = os.statvfs(self.location)
+                return stfs.f_blocks * stfs.f_frsize
+            else:
+                return self.storage_max
         finally:
             self.lock.release()
             
@@ -304,10 +308,17 @@ class XendStorageRepository:
         """
         self.lock.acquire()
         try:
-            total_used = 0
-            for val in self.images.values():
-                total_used += val.physical_utilisation
-            return total_used
+            return self.storage_used
+        finally:
+            self.lock.release()
+
+    def virtual_allocation(self):
+        """Returns the total virtual space allocated within the storage repo.
+        @rtype: int
+        """
+        self.lock.acquire()
+        try:
+            return self.storage_alloc
         finally:
             self.lock.release()